import { type App } from 'vue'
import { ElLoading } from 'element-plus'
import {
  ApolloClient,
  createHttpLink,
  InMemoryCache
} from '@apollo/client/core'

// 挂载全局方法
export default {
  install: (app: App) => {
    let loadingInstance: any
    app.config.globalProperties.$loadingStart = () => {
      loadingInstance = ElLoading.service({
        body: true,
        fullscreen: true,
        text: '拼命加载中',
        customClass: 'loading-class',
        background: 'rgba(255, 255, 255, 0.3)'
      })
    }
    app.config.globalProperties.$loadingEnd = () => {
      loadingInstance?.close()
    }

    // 与 API 的 HTTP 连接
    const httpLink = createHttpLink({
      // 你需要在这里使用绝对路径
      uri: 'http://localhost:3000/graphql'
    })
    // 缓存实现
    const cache = new InMemoryCache()
    // 创建 apollo 客户端
    const apolloClient = new ApolloClient({
      link: httpLink,
      cache
    })
    app.config.globalProperties.$apollo = apolloClient
  }
}
